/**
 * Created by caelan on 2018/10/20.
 */

var thresholdRange = {

  html: function (range,layName) {
      // var html = '<div class="pluginsDiv" id="thresholdRangeDiv">';
      if(layName!="要素类" && layName!="土壤类" && layName!="评价类" || Global.ElementName=="光温水"){
          var html = '<div class="pluginsDiv" id="thresholdRangeDiv" style="display: none">';
      }else{
          if(Global.currElementObj.name == "稳定通过界限温度"){
            var html = '<div class="pluginsDiv" id="thresholdRangeDiv" style="display: none">';
          }else{
            var html = '<div class="pluginsDiv" id="thresholdRangeDiv">';
          }
         
      }


    var isChecked = (range.selected === 1 ? "checked": "");
    html += '<input type="checkbox" lay-filter="thresholdRange" id="thresholdSelected" lay-skin="primary" '+ isChecked +' > <span style="margin-left:10px;margin-top:10px;">设定阈值</span>';

    var maxType = ['<=','<'].map(function (val) {
      return '<option '+ ( range.maxType === val ? 'selected="true"' : '') +' value="'+ val +'">'+ val +'</option>';
    });
    var minType = ['>=','>','='].map(function (val) {
      return '<option '+ ( range.minType === val ? 'selected="true"' : '') +' value="'+ val +'">'+ val +'</option>';
    });

    html += '<div class="layui-form-item pluginsSingleRows" style="margin-top:10px;">' +
        '<label class="layui-form-label">最小值:</label> ' +
        '<div class="layui-input-inline"> ' +
          '<select lay-filter="minTypeEqual" name="price_min_type">'+ minType +'</select>' +
        '</div> ' +
        '<div class="layui-input-inline"> ' +
          '<input id="rangeMin" type="number" ' + (range.selected === 1 ? "" : "disabled") + ' ' +
          'data-default="' + range.min + '" name="price_min" min="' + range.min + '" ' +
          'max="' + range.max + '" value="' + range.min + '" autocomplete="off" ' +
          'class=" ' + (range.selected === 1 ? "" : "layui-disabled") + ' layui-input"> ' +
        '</div> ' +
      '</div>' +
      '<div class="layui-form-item pluginsSingleRows">' +
        '<label class="layui-form-label">最大值:</label> ' +
        '<div class="layui-input-inline"> ' +
          '<select name="price_max_type">'+ maxType +'</select>' +
        '</div> ' +
        '<div class="layui-input-inline"> ' +
          '<input id="rangeMax" type="number" ' + (range.selected === 1 ? "" : "disabled") + ' ' +
          'data-default="' + range.max + '" name="price_max" min="' + range.min + '" ' +
          'max="' + range.max + '" value="' + range.max + '" autocomplete="off" ' +
          'class=" ' + (range.selected === 1 ? "" : "layui-disabled") + ' layui-input"> ' +
        '</div> ' +
      '</div>';

    html += '</div>';
    return html;
  },
  getData: function () {
    var min = $('#rangeMin'), max = $('#rangeMax'), checkbox = $('#thresholdSelected');
    Global.postData.leftArray = ( checkbox.prop('checked') === false || min.val() === '' ) ? [] : [min.val()];
    Global.postData.rightArray = ( checkbox.prop('checked') === false || max.val() === '' ) ? [] : [max.val()];

    Global.postData.leftOpers = [$('#thresholdRangeDiv select[name="price_min_type"]').val()];
    Global.postData.rightOpers = [$('#thresholdRangeDiv select[name="price_max_type"]').val()];
  },
  renderCallback: function () {

    var minType = $('#thresholdRangeDiv select[name="price_min_type"]');

    var minDisabled = function (d) {
      $('#rangeMin').prop('disabled', d);
      minType.prop('disabled', d);
    };
    var maxDisabled = function (d) {
      $('#rangeMax').prop('disabled', d);
      $('#thresholdRangeDiv select[name="price_max_type"]').prop('disabled', d);
    };

    if ( minType.val() === '=' ) {
      maxDisabled(true);
      layForm.render(null, 'middleDiv');
    }


    /*范围勾选可禁用*/
    layForm.on('checkbox(thresholdRange)', function(data){
      minDisabled(!data.elem.checked);
      if ( minType.val() === '=' && data.elem.checked ) {
        maxDisabled(true);
      } else {
        maxDisabled(!data.elem.checked);
      }

      layForm.render(null, 'middleDiv');
    });

    /*最小值选择等于判断*/
    layForm.on('select(minTypeEqual)', function (data) {
      maxDisabled(data.value === '=');
      layForm.render(null, 'middleDiv');
     });



  }

};